<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in simpleList" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartment, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'VueAdminTemplateDeptDialog',
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    orignList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      // console.log('正在输入的部门编码是', value)
      let CodeList = null
      if (this.isEdit) {
        // 过滤掉当前的部门, map得到部门编号列表
        CodeList = this.orignList.filter(item => item.id !== this.id).map(item => item.code)
      } else {
        // map得到部门编号列表
        CodeList = this.orignList.map(item => item.code)
        console.log(CodeList)
      }
      CodeList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    const validName = (rule, value, callback) => {
      // 过滤掉pid相同的同级部门
      const nameList = this.orignList.filter(item => item.pid === this.id)
      // console.log(nameList)
      nameList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '输入的部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '输入的名称在1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '输入的部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '输入的部门编码在1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '输入的部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门的介绍不可以为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门的介绍在1-300个字符之间', trigger: 'blur' }
        ]
      },
      simpleList: []
    }
  },
  created() {
    // 员工简易列表
    this.loadsimple()
    //  获取部门详情
    this.loadDetail()
  },
  methods: {
    // 获取部门详情
    async loadDetail() {
      // 如果不是编辑状态，中断
      if (!this.isEdit) return
      // 发请求
      const res = await getDepartDetail(this.id)
      // console.log(res)
      // 数据回填
      this.form = res.data
    },
    // 获取简单员工列表
    async loadsimple() {
      const res = await getEmployeeSimple()
      // console.log(res)
      this.simpleList = res.data
    },
    // 添加或编辑操作
    hSubmit() {
    // 表单验证，兜底校验
      this.$refs.deptForm.validate(valid => {
        // console.log(valid)
        if (!valid) return
        // 如果兜底校验通过的话，就判断是在编辑状态还是添加状态
        this.isEdit ? this.toEdit() : this.toAdd()
      })
    },
    // 添加
    async toAdd() {
      try {
        // 发请求
        this.form.pid = this.id
        const res = await addDepartment(this.form)
        // console.log(res)
        // 提醒用户
        this.$message.success(res.message)
        // 通知父组件关闭弹框   重新渲染页面数据
        this.$emit('success')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 编辑
    async toEdit() {
      try {
        // 发请求
        const res = await updateDepartments(this.form)
        // console.log(res)
        this.$message.success(res.message)
        // 通知父组件关闭弹框   重新渲染页面数据
        this.$emit('success')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hCancel() {
      // 通知父组件关闭弹窗
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
